package light.mvc.service.patient.impl;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import light.mvc.dao.BaseDaoI;
import light.mvc.model.admin.Tuser;
import light.mvc.model.item.Ttreatment;
import light.mvc.model.register.Tregister;
import light.mvc.model.register.TregisterTreatment;
import light.mvc.page.base.PageFilter;
import light.mvc.page.patient.Register;
import light.mvc.service.patient.MyRegisterServiceI;

import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class MyRegisterServiceImpl implements MyRegisterServiceI {

	@Autowired
	private BaseDaoI<Tregister> registerDao;
	
	@Autowired
	private BaseDaoI<Tuser> userDao;
	
	@Autowired 
	private BaseDaoI<TregisterTreatment> registerTreatmentDao;
	
	@Autowired
	private BaseDaoI<Ttreatment> treatmentDao;
	
	@Override
	public Register detail(Integer registerId) {
		Tregister t = registerDao.get(Tregister.class,registerId);
		
		Register register = new Register();
		BeanUtils.copyProperties(t, register);
		
		return register;
	}
	
	@Override
	public Integer edit(Integer registerId) throws Exception{
		Tregister t = registerDao.get(Tregister.class,registerId);

		Date now = new Date();
		Date timeSegment = t.getTimeSegment();
		Long oneHour = 1L * 60 * 60 * 1000;
		Long fourHour = -4 * oneHour;
		
		Long timeDistance = timeSegment.getTime() - now.getTime();
		if(timeDistance >  oneHour || timeDistance < fourHour) {
			throw new Exception("请在就诊时间点的前一小时内或后四小时内取号");
		}
		
		Map<String,Object> params = new HashMap<String,Object>();
		params.put("registerId", registerId);
		String hql = "select seq from Tregister t where t.id = :registerId";
		Integer seq = registerDao.countInteger(hql, params);
		if(seq != null) {
			return seq;
		}
		
		params.remove("registerId");
		params.put("timeSegment", t.getTimeSegment());
		hql = "select max(seq) from Tregister t where t.timeSegment = :timeSegment";
		seq = registerDao.countInteger(hql,params);
		
		if(seq == null) {
			seq = 1;
		} else {
			seq++;
		}

		t.setSeq(seq);
		t.setRegisterTime(new Date());
		registerDao.update(t);
		
		TregisterTreatment registerTreatment = new TregisterTreatment();
		registerTreatment.setRegister(t);
		registerTreatment.setTreatment(treatmentDao.get(Ttreatment.class, 1));
		registerTreatment.setChargeTime(new Date());
		registerTreatmentDao.save(registerTreatment);
		
		return seq;
		
	}
	
	@Override
	public List<Register> dataGrid(Integer patientId,Register registerSome, PageFilter ph) {
		List<Register> registers = new ArrayList<Register>();
		Map<String, Object> params = new HashMap<String, Object>();
		params.put("patientId", patientId);
		String hql = "select t from Tregister t join t.patient patient where patient.id = :patientId";
		List<Tregister> list = registerDao.find(hql + orderHql(ph), params, ph.getPage(), ph.getRows());
		
		for (Tregister t : list) {
			Register register = new Register();
			register.setId(t.getId());
			register.setDoctorName(t.getDoctor().getName());
			register.setDepartmentName(t.getDoctor().getDepartment().getName());
			register.setType(t.getType());
			register.setReserveTime(t.getReserveTime());
			register.setRegisterTime(t.getRegisterTime());
			register.setTimeSegment(t.getTimeSegment());
			register.setSeq(t.getSeq());
			registers.add(register);
		}
		return registers;
	}

	private String orderHql(PageFilter ph) {
		String orderString = "";
		if ((ph.getSort() != null) && (ph.getOrder() != null)) {
			orderString = " order by t." + ph.getSort() + " " + ph.getOrder();
		}
		return orderString;
	}
	
	@Override
	public Long count(Integer patientId, PageFilter ph) {
		Map<String, Object> params = new HashMap<String, Object>();
		params.put("patientId", patientId);
		String hql = "from Tregister t join t.patient patient where patient.id = :patientId";
		
		
		return registerDao.count("select count(*) " + hql + orderHql(ph), params);
	}
}
